﻿
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Net;
using System.Web;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;

namespace PayPal
{
	public class PayPalEcomBizRules : Syrinx.BaseBizRules
	{
		public IBizObj updatePayPalPaymentInfoSalesOrderPayment
			(IApplication app, IRuleContext context, string returnUrl, IBizObj salesOrder, IBizObj paymentTerm)
		{
			IBizObj controlInfo = app.bizObjMgr.create("eComPaymentControlInfo");
			bool paymentSuccess = string.Compare((string)paymentTerm.get("paymentStatus"), "completed", true) == 0;
			paymentTerm.put("id", salesOrder.get("id"));
			int ru = (int)app.appData.get("DA.PayPal.AddPayPalPaymentInfo", paymentTerm);
			if (ru != 1) //This should be a real problem because that means the add didnt happen, probably because there is a row in the db with the same id.
			{
				//TODO: Should we do an update now???
			}
			foreach (IBizObj recurInfo in (IBizObjList)paymentTerm.get("recurPayments"))
			{
				ru = (int)app.appData.get("DA.PayPal.AddPayPalRecurringProfile", recurInfo);
			}

			controlInfo.put("paymentSuccessful", paymentSuccess);
			return controlInfo;
		}

		public IBizObj getPayPalPaymentInfoPaymentDetails(IApplication app, IRuleContext context, Swaf.BGuid.Guid salesOrderId)
		{
			FlexiMap m = new FlexiMap();
			m.put("salesOrderId", salesOrderId);
			IBizObj termDetails =  app.appData.get("DA.PayPal.GetPayPalPaymentInfo", m) as IBizObj;
			return termDetails;
		}


		//The following two bizrules reuse code from ecom (copy/paste method).
		public IBizObj updatePayPalCreditCardPaymentInfoSalesOrderPayment
			(IApplication app, IRuleContext context, string returnUrl, IBizObj salesOrder, IBizObj paymentTerm)
		{
			IBizObj controlInfo = app.bizObjMgr.create("eComPaymentControlInfo");
			controlInfo.put("paymentSuccessful", false);

			IResults rc = context.RuleGroup.execute(context, "doPayPalDirectPayment", salesOrder, paymentTerm);
			IBizObj paypalInfo = rc.CallResults as IBizObj;
			if (paypalInfo != null)
			{
				if (Cmn.IsEmpty(paypalInfo.get("error.code")))
				{
					DateTime expireDate = (DateTime)paymentTerm.get("expireDate");
					if (expireDate == null || expireDate == DateTime.MinValue)
						paymentTerm.put("expireDate", DateTime.Now.ToUniversalTime().Date);
					FlexiMap m = new FlexiMap();
					m.put("order", salesOrder);
					m.put("payment", paymentTerm);
					string ccNum = paymentTerm.get("cardNumber").ToString();
					if (ccNum.Length > 5)
					{
						StringBuilder buff = new StringBuilder();
						for (int p = 0; p < ccNum.Length - 4; p++)
							buff.Append("*");
						buff.Append(ccNum.Substring(ccNum.Length - 4));
						paymentTerm.put("cardNumber", buff.ToString());
					}
					app.appData.put("DA.eCom.DeleteSalesOrderPaymentInfo", m);
					app.appData.put("DA.eCom.AddSalesOrderPaymentInfo", m);
					app.appData.put("DA.eCom.UpdateSalesOrderPaymentTermsId", m);

					controlInfo.put("paymentSuccessful", true);
				}
				else
				{
					FlexiMap m = (FlexiMap)controlInfo.get("errorInfo");
					m["error"] = paypalInfo.get("error");
					controlInfo.put("errorMessageId", "Error-PayPalFailure");
				}
			}
			return controlInfo;
		}

		public IBizObj getPayPalCreditCardPaymentInfoPaymentDetails(IApplication app, IRuleContext context, Swaf.BGuid.Guid salesOrderId)
		{
			FlexiMap m = new FlexiMap();
			m.put("id", salesOrderId);
			IBizObj termDetails = app.appData.get("DA.eCom.GetBasicCreditCardPaymentInfo", m) as IBizObj;
			return termDetails;
		}

	}
}
